package cn.imokay.stopthread;

import org.junit.Test;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;

/**
 * run方法没有sleep和wait
 */
public class WrongWayStopThread {

    private volatile boolean stopped = false;



   // @Test
    public void stopTest() throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 5; i++) {
                    System.out.println("第" + i + "组开始");
                    for (int i1 = 0; i1 < 10; i1++) {
                        System.out.println("第" + i1 + "个");
                        try {
                            Thread.sleep(50);
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }

                    }
                    System.out.println("第" + i + "组结束");
                }
            }
        });
        thread.start();
        Thread.sleep(1000);
        //通知中断
        //thread.interrupt();
        //不推荐使用，线程会戛然而止，导致一组原子的操作都不会被执行完
        thread.stop();
        //thread.suspend();
        //thread.resume();
    }

    @Test
    public void volatileCanStop() throws InterruptedException {
        Thread thread = new Thread(new Runnable() {
            @Override
            public void run() {
                int num = 0;
                while (num < 10000 && !stopped) {
                    if (num % 100 == 0) {
                        System.out.println(num+"是100的整数倍");
                    }
                    num++;
                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                System.out.println("finished");
            }
        });

        thread.start();
        Thread.sleep(5000);
        stopped = true;
        while(true){}
    }

    /**
     * 陷入阻塞，生产速度快，消费速度慢，阻塞队列满了后，生产者阻塞，等待消费者消费    volatileCanNotStop
     */
    @Test
    public void Consumer() throws InterruptedException {
        BlockingQueue storge = new ArrayBlockingQueue<Integer>(10) {};
        Producer producer = new Producer(storge);

        Thread thread = new Thread(producer);
        thread.start();

        Thread.sleep(100);

        while(Math.random() <0.95){
            System.out.println("消费-----："+storge.take());
            Thread.sleep(50);
        }

        System.out.println("消费者已停止，不需要更多数据");

        //该标记位不再适用
        producer.stopped = true;
        //次方法可以永远相信，能唤醒阻塞，睡眠
        thread.interrupt();

        boolean interrupted = Thread.interrupted();

        while(true){

        }

    }

    /**
     * 生产者生产数据
     */
    class Producer implements Runnable {

        private BlockingQueue storge;

        public volatile boolean stopped = false;

        public Producer(BlockingQueue storge) {
            this.storge = storge;
        }

        @Override
        public void run() {
            int num = 0;
            try {
                while((!stopped && num < 10000) || !Thread.currentThread().isInterrupted()) {
                    if (num % 100 == 0) {
                        System.out.println("生产："+num);
                        storge.put(num);
                    }
                    num ++;
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } finally {
                System.out.println("Producer finished");
            }

        }
    }

}
